﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Kaixin.House.Ranch
{
    using Kaixin.Tools;

    public class Animal
    {
        private Network _network;
        private string _uid;           // your uid of this 
        private Friend _friend = null; // friend (owner) of this animal
        private string _verify;        // verify code

        public string AnimalSid;
        public string Name;
        public int Status = 0;         // grow status: 0:growing, 1:can product, 2: can harvest
        public int BStatus = -1;       // product status: -1: can not, 1: can, 2: can harvest
        public int FStatus = 0;        // food status 1: hungry

        public Animal(string uid, Friend friend, string verify,
            Network network)
        {
            _uid = uid;
            _friend = friend;
            _verify = verify;
            _network = network;
        }

        public bool CanHarvest()
        {
            if (_uid.Equals(_friend.Uid) && Status == 2)
                return true;
            else
                return false;
        }

        public bool CanProduct()
        {
            if (BStatus == 1)
                return true;
            else
                return false;
        }

        public bool Hungry()
        {
            return (FStatus == 1);
        }

        public bool Product()
        {
            string url = Consts.RootURL + Consts.RanchProductUrl +
                "?animalsid=" + AnimalSid + "&&verify=" + _verify + "&fuid=" + _friend.Uid;

            try
            {
                string respXML = _network.CallUrl(url);
                XMLParser parser = new XMLParser(respXML);
                if ("succ".Equals(parser.GetNodeValue("/data/ret")))
                {
                    TextOutput.PrintLine("Product: " + _friend.Name + " " + Name + " "
                        + "succ");
                    return true;
                }
                else
                {
                    TextOutput.PrintLine("Product: " + _friend.Name + " " + Name + " "
                        + "Fail " + parser.GetNodeValue("/data/reason"));
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Harvest()
        {
            string url = Consts.RootURL + Consts.RanchAnimalHarvestUrl +
                "?animalsid=" + AnimalSid + "&&verify=" + _verify + "&fuid=" + _friend.Uid;

            try
            {
                string respXML = _network.CallUrl(url);
                XMLParser parser = new XMLParser(respXML);
                if ("succ".Equals(parser.GetNodeValue("/data/ret")))
                {
                    TextOutput.PrintLine("Harvest: " + _friend.Name + " " + Name + " "
                        + "succ");
                    return true;
                }
                else
                {
                    TextOutput.PrintLine("Harvest: " + _friend.Name + " " + Name + " "
                        + "Fail " + parser.GetNodeValue("/data/reason"));
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
